﻿Imports DotNetNuke.Services.Localization
Imports Aricie.DNN.Modules.LuceneSearch.Settings

Public Enum PagerButtonType
    PageFirst = 0
    PagePrevious = 1
    PageView = 2
    PageNext = 3
    PageLast = 4
    Other = 5
End Enum

Public Class GridviewPager

    Public Event NextPage As EventHandler
    Public Event PreviousPage As EventHandler
    Public Event ViewPage As EventHandler
    Public Event FirstPage As EventHandler
    Public Event LastPage As EventHandler

    Public Function GetPager(ByVal currentIndex As Integer, ByVal nbTotalPages As Integer, ByVal pagesVisited As List(Of Integer), ByVal localResourceFile As String, ByVal pagerStyle As PagerButtonsStyle, Optional ByVal nbPagesMax As Integer = 10) As Dictionary(Of Control, PagerButtonType)
        Dim firstWord As String = GetPageButtonName(localResourceFile, pagerStyle, PagerButtonType.PageFirst)
        Dim previousWord As String = GetPageButtonName(localResourceFile, pagerStyle, PagerButtonType.PagePrevious)
        Dim pageWord As String = GetPageButtonName(localResourceFile, pagerStyle, PagerButtonType.PageView)
        Dim nextWord As String = GetPageButtonName(localResourceFile, pagerStyle, PagerButtonType.PageNext)
        Dim lastWord As String = GetPageButtonName(localResourceFile, pagerStyle, PagerButtonType.PageLast)

        Dim displayNextPrevious As Boolean = DisplayPreviousNextButtons(pagerStyle)
        Dim displayFirstLast As Boolean = DisplayFirstLastButtons(pagerStyle)
        Dim displayPage As Boolean = DisplayPagesButtons(pagerStyle)

        Return GetPager(currentIndex, nbTotalPages, displayNextPrevious, displayFirstLast, displayPage, previousWord, nextWord, pageWord, firstWord, lastWord, pagesVisited, nbPagesMax)
    End Function

    Public Function GetPager(ByVal currentIndex As Integer, ByVal nbTotalPages As Integer, ByVal displayNextPrevious As Boolean, ByVal displayFirstLast As Boolean, ByVal displayPage As Boolean, ByVal previousWord As String, ByVal nextWord As String, ByVal pageWord As String, ByVal firstWord As String, ByVal lastWord As String, ByVal pagesVisited As List(Of Integer), Optional ByVal nbPagesMax As Integer = 10) As Dictionary(Of Control, PagerButtonType)
        Dim listControls As New Dictionary(Of Control, PagerButtonType)

        Dim iBegin As Integer = CInt(currentIndex - (nbPagesMax / 2)) + 1
        Dim iLast As Integer = CInt(currentIndex + (nbPagesMax / 2)) + 1

        listControls.Add(New LiteralControl("<div id='pagerDiv' class='LucenePager pager'>"), PagerButtonType.Other)

        'Button Previous
        If Not (currentIndex = 0) Then

            If displayFirstLast Then
                Dim btnFirst As New LinkButton
                btnFirst.ID = "First"
                btnFirst.Text = firstWord
                btnFirst.CssClass = "PageNavigation PageFirst"
                AddHandler btnFirst.Click, AddressOf _FirstPage
                listControls.Add(btnFirst, PagerButtonType.PageFirst)
            End If

            If displayNextPrevious Then
                Dim btnPrevious As New LinkButton
                btnPrevious.ID = "Previous"
                btnPrevious.Text = previousWord
                btnPrevious.CssClass = "PageNavigation PagePrevious"
                AddHandler btnPrevious.Click, AddressOf _PreviousPage
                listControls.Add(btnPrevious, PagerButtonType.PagePrevious)
            End If
        End If


        If iBegin < 1 Then
            iBegin = 1
            iLast = nbPagesMax
        End If
        If iLast > nbTotalPages Then
            iLast = nbTotalPages
            iBegin = nbTotalPages - nbPagesMax
        End If

        If displayPage Then
            'Buttons Pages
            For i As Integer = iBegin To iLast
                If i > 0 And i <= nbTotalPages Then
                    Dim btnPage As New LinkButton
                    btnPage.ID = "Page_" + i.ToString
                    btnPage.Text = pageWord + " " + i.ToString
                    If i = currentIndex + 1 Then
                        btnPage.CssClass = "PageNavigation PageSelected"
                        btnPage.Enabled = False

                        'VisitedCss est inopérant du fait que le pager est recrée à chaque chargement
                    ElseIf pagesVisited.Contains(i) Then
                        btnPage.CssClass = "PageNavigation PageSelectedVisited"
                    Else
                        btnPage.CssClass = "PageNavigation PageNotSelected"
                    End If

                    AddHandler btnPage.Click, AddressOf _ViewPage
                    listControls.Add(btnPage, PagerButtonType.PageView)
                End If
            Next
        End If

        'Button Next
        If Not (currentIndex = nbTotalPages - 1) Then

            If displayNextPrevious Then
                Dim btnNext As New LinkButton
                btnNext.ID = "Next"
                btnNext.Text = nextWord
                btnNext.CssClass = "PageNavigation PageNext"
                AddHandler btnNext.Click, AddressOf _NextPage
                listControls.Add(btnNext, PagerButtonType.PageNext)
            End If

            If displayFirstLast Then
                Dim btnLast As New LinkButton
                btnLast.ID = "Last"
                btnLast.Text = lastWord
                btnLast.CssClass = "PageNavigation PageLast"
                AddHandler btnLast.Click, AddressOf _LastPage
                listControls.Add(btnLast, PagerButtonType.PageLast)
            End If

        End If

        listControls.Add(New LiteralControl("</div>"), PagerButtonType.Other)

        Return listControls
    End Function


    <Obsolete("22/02/2010 Use Others GetPager() Signatures ")> _
    Public Function GetPager(ByVal currentIndex As Integer, ByVal nbTotalPages As Integer, ByVal previousWord As String, ByVal nextWord As String, ByVal pageWord As String, ByVal pagesVisited As List(Of Integer), Optional ByVal nbPagesMax As Integer = 10) As List(Of Control)
        Dim dicoControls As Dictionary(Of Control, PagerButtonType) = GetPager(currentIndex, nbTotalPages, True, True, True, previousWord, nextWord, pageWord, String.Empty, String.Empty, pagesVisited, nbPagesMax)
        Dim toReturn As New List(Of Control)
        For Each ctl As Control In dicoControls.Keys
            toReturn.Add(ctl)
        Next
        Return toReturn
    End Function


#Region "Private Methods"
    ''' <summary>
    ''' Localize Pager Buttons Names
    ''' </summary>
    ''' <param name="localResourceFile"></param>
    ''' <param name="pagerStyle"></param>
    ''' <param name="pagerButton"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function GetPageButtonName(ByVal localResourceFile As String, ByVal pagerStyle As PagerButtonsStyle, ByVal pagerButton As PagerButtonType) As String
        Dim toReturn As String
        Select Case pagerStyle
            Case PagerButtonsStyle.NextPrevious, PagerButtonsStyle.NextPreviousFirstLast, PagerButtonsStyle.Numeric, PagerButtonsStyle.NumericFirstLast
                toReturn = Localization.GetString("NotTextual" & pagerButton.ToString() & ".Text", localResourceFile)
            Case Else
                toReturn = Localization.GetString("Textual" & pagerButton.ToString() & ".Text", localResourceFile)
        End Select
        Return toReturn
    End Function

    ''' <summary>
    ''' Display Previous and Next Buttons ?
    ''' </summary>
    ''' <param name="pagerStyle"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DisplayPreviousNextButtons(ByVal pagerStyle As PagerButtonsStyle) As Boolean
        Dim toReturn As Boolean = False

        Select Case pagerStyle
            Case PagerButtonsStyle.NextPrevious, PagerButtonsStyle.NextPreviousFirstLast, PagerButtonsStyle.TextualNextPrevious, PagerButtonsStyle.TextualNextPreviousFirstLast, PagerButtonsStyle.TextualNumericNextPrevious, PagerButtonsStyle.TextualNumericNextPreviousFirstLast
                toReturn = True
            Case Else
                toReturn = False
        End Select

        Return toReturn
    End Function

    ''' <summary>
    ''' Display First and Last Buttons ?
    ''' </summary>
    ''' <param name="pagerStyle"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DisplayFirstLastButtons(ByVal pagerStyle As PagerButtonsStyle) As Boolean
        Dim toReturn As Boolean = False

        Select Case pagerStyle
            Case PagerButtonsStyle.NextPreviousFirstLast, PagerButtonsStyle.NumericFirstLast, PagerButtonsStyle.TextualNextPreviousFirstLast, PagerButtonsStyle.TextualNumericFirstLast, PagerButtonsStyle.TextualNumericNextPreviousFirstLast
                toReturn = True
            Case Else
                toReturn = False
        End Select

        Return toReturn
    End Function

    ''' <summary>
    ''' Display Pages Buttons ?
    ''' </summary>
    ''' <param name="pagerStyle"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function DisplayPagesButtons(ByVal pagerStyle As PagerButtonsStyle) As Boolean
        Dim toReturn As Boolean = False

        Select Case pagerStyle
            Case PagerButtonsStyle.Numeric, PagerButtonsStyle.NumericFirstLast, PagerButtonsStyle.TextualNumeric, PagerButtonsStyle.TextualNumericFirstLast, PagerButtonsStyle.TextualNumericNextPrevious, PagerButtonsStyle.TextualNumericNextPreviousFirstLast
                toReturn = True
            Case Else
                toReturn = False
        End Select

        Return toReturn
    End Function
#End Region

#Region "RaiseEvents"

    Private Sub _PreviousPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent PreviousPage(sender, e)
    End Sub

    Private Sub _NextPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent NextPage(sender, e)
    End Sub

    Private Sub _ViewPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent ViewPage(sender, e)
    End Sub

    Private Sub _FirstPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent FirstPage(sender, e)
    End Sub

    Private Sub _LastPage(ByVal sender As Object, ByVal e As System.EventArgs)
        RaiseEvent LastPage(sender, e)
    End Sub
#End Region

End Class
